package com.bbzn.device.client.service.emq.order.impl.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.constant.DeviceConstant;
import com.bbzn.device.client.dataobject.Contact;
import com.bbzn.device.client.dataobject.Device;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.ContactService;
import com.bbzn.device.client.service.DeviceService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.OptionalBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author wulongbo
 * @Date 2021/12/16 16:19
 * @Version 1.0
 */
@Component
public class MqSetContactImpl extends BaseMqSendImpl {

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private ContactService contactService;

    @Override
    public String checkMap(String language) throws Exception {
        if (!OptionalBean.ofNullable(map.get("contactList")).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NU_OXYUPPER_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(map.get("isOpen")).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NU_OXYLOWER_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Contact contact = new Contact();
        contact.setImei(map.get("imei").toString());
        contact.setContactList(JSON.toJSONString(map.get("contactList")));
        contact.setIsOpen(Integer.parseInt(map.get("isOpen").toString()));
        contact.setCreateTime(new Date());
        contactService.insertOrUpdateSelective(contact);
        return JSONObject.toJSONString(map);
    }
}
